/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Model.ManageDB;
import Model.TypeDAO;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;

/**
 *
 * @author Romain
 */
public class mainFrame extends javax.swing.JFrame {

  TypeDAO typeDAO;
  /**
   * Creates new form mainFrame
   */
  Connection connect;

  public mainFrame(Connection connect) {
	initComponents();
	this.connect = connect;
	typeDAO = new TypeDAO(connect);

	Map<String, Controller.Type> types = new HashMap();

	types = typeDAO.findAll();
	for (String mapKey : types.keySet()) {
	  // utilise ici hashMap.get(mapKey) pour accéder aux valeurs
	  cboTypeMateriel.addItem(types.get(mapKey));
	}
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jSeparator1 = new javax.swing.JSeparator();
    cboTypeMateriel = new javax.swing.JComboBox();
    bCreerMateriel = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    bInventaire = new javax.swing.JButton();
    jMateriel = new javax.swing.JToggleButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    cboTypeMateriel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cboTypeMaterielActionPerformed(evt);
      }
    });

    bCreerMateriel.setText("Créer");
    bCreerMateriel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bCreerMaterielActionPerformed(evt);
      }
    });

    jLabel1.setText("Matériel");

    jLabel2.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
    jLabel2.setText("Semi-Automatisation de la Gestion des Stocks");

    bInventaire.setText("Inventaire");
    bInventaire.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bInventaireActionPerformed(evt);
      }
    });

    jMateriel.setText("Materiel");
    jMateriel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMaterielActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(79, 79, 79)
        .addComponent(jSeparator1))
      .addGroup(layout.createSequentialGroup()
        .addGap(43, 43, 43)
        .addComponent(jLabel2)
        .addContainerGap(51, Short.MAX_VALUE))
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(cboTypeMateriel, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(bCreerMateriel)
            .addGap(7, 7, 7))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(jMateriel)
            .addGap(26, 26, 26)
            .addComponent(bInventaire)
            .addGap(24, 24, 24))))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(jLabel2)
        .addGap(20, 20, 20)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(cboTypeMateriel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(bCreerMateriel)
          .addComponent(jLabel1))
        .addGap(16, 16, 16)
        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(41, 41, 41)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(bInventaire)
          .addComponent(jMateriel))
        .addContainerGap(153, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void cboTypeMaterielActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboTypeMaterielActionPerformed
	// TODO add your handling code here:
  }//GEN-LAST:event_cboTypeMaterielActionPerformed

  private void bCreerMaterielActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCreerMaterielActionPerformed
	// TODO add your handling code here:
	Controller.Type type = (Controller.Type) cboTypeMateriel.getSelectedItem();

	if (type.getIdT().equals("001")) {
	  JFrame jfMat = new jframeMateriel(connect, type);
          jfMat.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          jfMat.setVisible(true);
	} else if (type.getIdT().equals("002")) {
	  JFrame jfOrdi = new jframeOrdinateur(connect, type);
          jfOrdi.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          jfOrdi.setVisible(true);
	} else if (type.getIdT().equals("003")) {
	  JFrame jfImp =new jframeImprimante(connect, type);
          jfImp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          jfImp.setVisible(true);
	} else if (type.getIdT().equals("004")) {
	  JFrame jfVideo = new jframeVideoproj(connect, type);
          jfVideo.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          jfVideo.setVisible(true);
	}
  }//GEN-LAST:event_bCreerMaterielActionPerformed

  private void bInventaireActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bInventaireActionPerformed
    // TODO add your handling code here:
	JFrame jfResultInv = new jframeResultInv(connect);
        jfResultInv.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jfResultInv.setVisible(true);
  }//GEN-LAST:event_bInventaireActionPerformed

  private void jMaterielActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMaterielActionPerformed
        JFrame jfVoireMat= new jframeVoirMateriel(connect);
        jfVoireMat.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jfVoireMat.setVisible(true);
  }//GEN-LAST:event_jMaterielActionPerformed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
	/* Set the Nimbus look and feel */
	//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
	 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
	 */
	try {
	  for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
		if ("Nimbus".equals(info.getName())) {
		  javax.swing.UIManager.setLookAndFeel(info.getClassName());
		  break;
		}
	  }
	} catch (ClassNotFoundException ex) {
	  java.util.logging.Logger.getLogger(mainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (InstantiationException ex) {
	  java.util.logging.Logger.getLogger(mainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (IllegalAccessException ex) {
	  java.util.logging.Logger.getLogger(mainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (javax.swing.UnsupportedLookAndFeelException ex) {
	  java.util.logging.Logger.getLogger(mainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	//</editor-fold>

	/* Create and display the form */
	java.awt.EventQueue.invokeLater(new Runnable() {
	  public void run() {
		try {
		  Connection conn = ManageDB.Open();
		  new mainFrame(conn).setVisible(true);
		} catch (Exception ex) {
		  ex.printStackTrace();
		}
	  }
	});
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton bCreerMateriel;
  private javax.swing.JButton bInventaire;
  private javax.swing.JComboBox cboTypeMateriel;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JToggleButton jMateriel;
  private javax.swing.JSeparator jSeparator1;
  // End of variables declaration//GEN-END:variables
}
